home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  20.2 KB  |  966 lines

  1. /* GNU test program (ksb and mjb) */
  2.  
  3. /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
  4.  
  5. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  6.  
  7. This file is part of GNU Bash, the Bourne Again SHell.
  8.  
  9. Bash is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free
  11. Software Foundation; either version 1, or (at your option) any later
  12. version.
  13.  
  14. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. You should have received a copy of the GNU General Public License along
  20. with Bash; see the file COPYING.  If not, write to the Free Software
  21. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  22.  
  23. /* Define STANDALONE to get the /bin/test version.  Otherwise, we are
  24.    making this for the shell. */
  25. /* #define STANDALONE */
  26.  
  27. #include <stdio.h>
  28.  
  29. #if !defined (sony)
  30. #include <fcntl.h>
  31. #endif
  32.  
  33. #include <sys/types.h>
  34. #include "general.h"
  35. #include "posixstat.h"
  36. #include <sys/file.h>
  37.  
  38. #if defined (USG) && defined (STANDALONE)
  39. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  40. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  41. #endif
  42.  
  43. #if !defined (R_OK)
  44. #define R_OK 4
  45. #define W_OK 2
  46. #define X_OK 1
  47. #define F_OK 0
  48. #endif /* R_OK */
  49.  
  50. /* The following few defines control the truth and false output of each stage.
  51.    TRUE and FALSE are what we use to compute the final output value.
  52.    SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
  53.    TRUTH_OR is how to do logical or with TRUE and FALSE.
  54.    TRUTH_AND is how to do logical and with TRUE and FALSE..
  55.    Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
  56.     SHELL_BOOLEAN = (!value). */
  57. #define TRUE 1
  58. #define FALSE 0
  59. #define SHELL_BOOLEAN(value) (!(value))
  60. #define TRUTH_OR(a, b) ((a) | (b))
  61. #define TRUTH_AND(a, b) ((a) & (b))
  62.  
  63. #if defined (STANDALONE)
  64. #  define test_exit(val) exit (val)
  65. #else
  66. #  include <setjmp.h>
  67.    static jmp_buf test_exit_buf;
  68.    static int test_error_return = 0;
  69. #  define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
  70. #endif /* STANDALONE */
  71.  
  72. #if defined (USG)
  73. static int sys_v = 1;
  74. #else
  75. static int sys_v = 0;
  76. #endif /* USG */
  77.  
  78. static int pos;        /* The offset of the current argument in ARGV. */
  79. static int argc;    /* The number of arguments present in ARGV. */
  80. static char **argv;    /* The argument list. */
  81.  
  82. static int unop ();
  83. static int binop ();
  84. static int unary_operator ();
  85. static int binary_operator ();
  86. static int two_arguments ();
  87. static int three_arguments ();
  88. static int posixtest ();
  89.  
  90. static int expr ();
  91. static int term ();
  92. static int and ();
  93. static int or ();
  94.  
  95. static void
  96. test_syntax_error (format, arg)
  97.      char *format, *arg;
  98. {
  99.   (void) fprintf (stderr, "%s: ", argv[0]);
  100.   (void) fprintf (stderr, format, arg);
  101.   fflush (stderr);
  102.   test_exit (SHELL_BOOLEAN (FALSE));
  103. }
  104.  
  105. static void
  106. test_io_error (name)
  107.      char *name;
  108. {
  109.   extern int errno;
  110.   int old_errno = errno;
  111.   fprintf (stderr, "%s: ", argv[0]);
  112.   errno = old_errno;
  113.   perror (name);
  114.   test_exit (SHELL_BOOLEAN (FALSE));
  115. }
  116.  
  117. /* Do the same thing access(2) does, but use the effective uid and gid,
  118.    and don't make the mistake of telling root that any file is
  119.    executable. */
  120.  
  121. static int
  122. eaccess (path, mode)
  123.      char *path;
  124.      int mode;
  125. {
  126.   struct stat st;
  127.   static int euid = -1;
  128. #if !defined (HAVE_MULTIPLE_GROUPS)
  129.   static int egid = -1;
  130. #endif
  131.  
  132.   if (stat (path, &st) < 0)
  133.     return (-1);
  134.  
  135.   if (euid == -1)
  136.     euid = geteuid ();
  137.  
  138. #if !defined (HAVE_MULTIPLE_GROUPS)
  139.   if (egid == -1)
  140.     egid = getegid ();
  141. #endif
  142.  
  143.   if (euid == 0)
  144.     {
  145.       /* Root can read or write any file. */
  146.       if (mode != X_OK)
  147.     return (0);
  148.  
  149.       /* Root can execute any file that has any one of the execute
  150.      bits set. */
  151.       if (st.st_mode & S_IXUGO)
  152.     return (0);
  153.     }
  154.  
  155.   if (st.st_uid == euid)        /* owner */
  156.     mode <<= 6;
  157.  
  158. #if defined (HAVE_MULTIPLE_GROUPS)
  159.   else if (group_member (st.st_gid))    /* group_member from execute_cmd.c */
  160. #else
  161.   else if (st.st_gid == egid)
  162. #endif
  163.     mode <<= 3;
  164.  
  165.   if (st.st_mode & mode)
  166.     return (0);
  167.  
  168.   return (-1);
  169. }
  170.   
  171. /* Increment our position in the argument list.  Check that we're not
  172.    past the end of the argument list.  This check is supressed if the
  173.    argument is FALSE.  Made a macro for efficiency. */
  174. #if !defined (lint)
  175. #define advance(f)    (++pos, f && (pos < argc ? 0 : beyond()))
  176. #endif
  177.  
  178. #if !defined (advance)
  179. static int
  180. advance (f)
  181.      int f;
  182. {
  183.   ++pos;
  184.  
  185.   if (f && pos >= argc)
  186.     beyond ();
  187. }
  188. #endif /* advance */
  189.  
  190. #define unary_advance() (advance (1),++pos)
  191.  
  192. /*
  193.  * beyond - call when we're beyond the end of the argument list (an
  194.  *    error condition)
  195.  */
  196. static int
  197. beyond ()
  198. {
  199.   test_syntax_error ("argument expected\n", (char *)NULL);
  200. }
  201.  
  202. /* Syntax error for when an integer argument was expected, but
  203.    something else was found. */
  204. static void
  205. int_expt_err (pch)
  206.      char *pch;
  207. {
  208.   test_syntax_error ("integer expression expected %s\n", pch);
  209. }
  210.  
  211. /* Convert the argument whose position in the argument vector is M,
  212.    stuffing the result into PL, a pointer to a long.  If the argument
  213.    is not an integer, then return 0, else return 1. */
  214. static int
  215. isint (m, pl)
  216.      int m;
  217.      long *pl;
  218. {
  219.   extern long atol ();
  220.   register char *pch;
  221.  
  222.   pch = argv[m];
  223.  
  224.   /* Skip leading whitespace characters. */
  225.   while (*pch == '\t' || *pch == ' ')
  226.     pch++;
  227.  
  228.   /* accept negative numbers but not '-' alone */
  229.   if ('-' == *pch)
  230.     if ('\000' == *++pch)
  231.       return (0);
  232.  
  233.   /* Accept and discard a leading `+' but not a `+' alone */
  234.   if ('+' == *pch)
  235.     if ('\000' == *++pch)
  236.       return (0);
  237.  
  238.   while ('\000' != *pch)
  239.     {
  240.       switch (*pch)
  241.     {
  242.     case '0':
  243.     case '1':
  244.     case '2':
  245.     case '3':
  246.     case '4':
  247.     case '5':
  248.     case '6':
  249.     case '7':
  250.     case '8':
  251.     case '9':
  252.       break;
  253.     default:
  254.       return (0);
  255.     }
  256.       ++pch;
  257.     }
  258.   *pl = atol (argv[m]);
  259.   return (1);
  260. }
  261.  
  262. /* Find the modify time of the file whose name is in argv[POSITION],
  263.    stuffing it into AGE, a pointer to a long.  Return non-zero if
  264.    successful, else zero. */
  265. static int
  266. age_of (position, age)
  267.      int position;
  268.      long *age;
  269. {
  270.   struct stat stat_buf;
  271.  
  272.   if (stat (argv[position], &stat_buf) < 0)
  273.     {
  274.       return (0);
  275.     }
  276.   *age = stat_buf.st_mtime;
  277.   return (1);
  278. }
  279.  
  280. /*
  281.  * term - parse a term and return 1 or 0 depending on whether the term
  282.  *    evaluates to true or false, respectively.
  283.  *
  284.  * term ::=
  285.  *    '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
  286.  *    '-'('L'|'x') filename
  287.  *     '-t' [ int ]
  288.  *    '-'('z'|'n') string
  289.  *    string
  290.  *    string ('!='|'=') string
  291.  *    <int> '-'(eq|ne|le|lt|ge|gt) <int>
  292.  *    file '-'(nt|ot|ef) file
  293.  *    '(' <expr> ')'
  294.  * int ::=
  295.  *    '-l' string
  296.  *    positive and negative integers
  297.  */
  298. static int
  299. term ()
  300. {
  301.   auto int value;
  302.  
  303.   if (pos >= argc)
  304.     beyond ();
  305.  
  306.   /* Deal with leading "not"'s. */
  307.   if ('!' == argv[pos][0] && '\000' == argv[pos][1])
  308.     {
  309.       value = FALSE;
  310.       while (pos < argc && '!' == argv[pos][0] && '\000' == argv[pos][1])
  311.     {
  312.       advance (1);
  313.       value ^= (TRUE);
  314.     }
  315.  
  316.       return (value ^ (term ()));
  317.     }
  318.  
  319.   /* A paren-bracketed argument. */  
  320.   if ('(' == argv[pos][0] && '\000' == argv[pos][1])
  321.     {
  322.       advance (1);
  323.       value = expr ();
  324.       if (')' != argv[pos][0] || '\000' != argv[pos][1])
  325.     test_syntax_error ("argument expected, found %s\n", argv[pos]);
  326.       advance (0);
  327.       return (TRUE == (value));
  328.     }
  329.  
  330.   /* are there enough arguments left that this could be dyadic? */
  331.   if ((pos + 3 <= argc) && binop (argv[pos+1]))
  332.     value = binary_operator ();
  333.  
  334.   /* Might be a switch type argument */
  335.   else if ('-' == argv[pos][0] && 0 == argv[pos][2])
  336.     {
  337.       if (unop (argv[pos][1]))
  338.     value = unary_operator ();
  339.       else
  340.     test_syntax_error ("%s: unary operator expected\n", argv[pos]);
  341.     }
  342.   else
  343.     {
  344.       value = 0 != strlen (argv[pos]);
  345.       advance (0);
  346.     }
  347.  
  348.   return value;
  349. }
  350.  
  351. static int
  352. binary_operator ()
  353. {
  354.   register int op;
  355.   struct stat stat_buf, stat_spare;
  356.   long int l, r, value;
  357.   /* Are the left and right integer expressions of the form '-l string'? */
  358.   int l_is_l, r_is_l;
  359.  
  360.   if ('-' == argv[pos][0] && 'l' == argv[pos][1] && 0 == argv[pos][2])
  361.     {
  362.       l_is_l = 1;
  363.       op = pos + 2;
  364.       advance (0);
  365.     }
  366.   else
  367.     {
  368.       l_is_l = 0;
  369.       op = pos + 1;
  370.     }
  371.  
  372.   if ('-' == argv[op + 1][0] && 'l' == argv[op + 1][1] && 0 == argv[op + 1][2])
  373.     {
  374.       r_is_l = 1;
  375.       advance (0);
  376.     }
  377.   else
  378.     r_is_l = 0;
  379.  
  380.   if ('-' == argv[op][0])
  381.     {
  382.       /* check for eq, nt, and stuff */
  383.       switch (argv[op][1])
  384.     {
  385.       default:
  386.           break;
  387.       case 'l':
  388.         if ('t' == argv[op][2] && '\000' == argv[op][3])
  389.           {
  390.         /* lt */
  391.         if (l_is_l)
  392.           l = strlen (argv[op - 1]);
  393.         else
  394.           {
  395.             if (!isint (op - 1, &l))
  396.               int_expt_err ("before -lt");
  397.           }
  398.  
  399.         if (r_is_l)
  400.           r = strlen (argv[op + 2]);
  401.         else
  402.           {
  403.             if (!isint (op + 1, &r))
  404.               int_expt_err ("after -lt");
  405.           }
  406.         pos += 3;
  407.         return (TRUE == (l < r));
  408.           }
  409.  
  410.         if ('e' == argv[op][2] && '\000' == argv[op][3])
  411.           {
  412.         /* le */
  413.         if (l_is_l)
  414.           l = strlen (argv[op - 1]);
  415.         else
  416.           {
  417.             if (!isint (op - 1, &l))
  418.               int_expt_err ("before -le");
  419.           }
  420.         if (r_is_l)
  421.           r = strlen (argv[op + 2]);
  422.         else
  423.           {
  424.             if (!isint (op + 1, &r))
  425.               int_expt_err ("after -le");
  426.           }
  427.         pos += 3;
  428.         return (TRUE == (l <= r));
  429.           }
  430.         break;
  431.  
  432.       case 'g':
  433.  
  434.         if ('t' == argv[op][2] && '\000' == argv[op][3])
  435.           {
  436.         /* gt integer greater than */
  437.         if (l_is_l)
  438.           l = strlen (argv[op - 1]);
  439.         else
  440.           {
  441.             if (!isint (op - 1, &l))
  442.               int_expt_err ("before -gt");
  443.           }
  444.         if (r_is_l)
  445.           r = strlen (argv[op + 2]);
  446.         else
  447.           {
  448.             if (!isint (op + 1, &r))
  449.               int_expt_err ("after -gt");
  450.           }
  451.         pos += 3;
  452.         return (TRUE == (l > r));
  453.           }
  454.  
  455.         if ('e' == argv[op][2] && '\000' == argv[op][3])
  456.           {
  457.         /* ge - integer greater than or equal to */
  458.         if (l_is_l)
  459.           l = strlen (argv[op - 1]);
  460.         else
  461.           {
  462.             if (!isint (op - 1, &l))
  463.               int_expt_err ("before -ge");
  464.           }
  465.         if (r_is_l)
  466.           r = strlen (argv[op + 2]);
  467.         else
  468.           {
  469.             if (!isint (op + 1, &r))
  470.               int_expt_err ("after -ge");
  471.           }
  472.         pos += 3;
  473.         return (TRUE == (l >= r));
  474.           }
  475.         break;
  476.  
  477.       case 'n':
  478.         if ('t' == argv[op][2] && '\000' == argv[op][3])
  479.           {
  480.         /* nt - newer than */
  481.         pos += 3;
  482.         if (l_is_l || r_is_l)
  483.           test_syntax_error ("-nt does not accept -l\n", (char *)NULL);
  484.         if (age_of (op - 1, &l) && age_of (op + 1, &r))
  485.           return (TRUE == (l > r));
  486.         else
  487.           return (FALSE);
  488.           }
  489.  
  490.         if ('e' == argv[op][2] && '\000' == argv[op][3])
  491.           {
  492.         /* ne - integer not equal */
  493.         if (l_is_l)
  494.           l = strlen (argv[op - 1]);
  495.         else
  496.           {
  497.             if (!isint (op - 1, &l))
  498.               int_expt_err ("before -ne");
  499.           }
  500.         if (r_is_l)
  501.           r = strlen (argv[op + 2]);
  502.         else
  503.           {
  504.             if (!isint (op + 1, &r))
  505.               int_expt_err ("after -ne");
  506.           }
  507.         pos += 3;
  508.         return (TRUE == (l != r));
  509.           }
  510.         break;
  511.  
  512.       case 'e':
  513.         if ('q' == argv[op][2] && '\000' == argv[op][3])
  514.           {
  515.         /* eq - integer equal */
  516.         if (l_is_l)
  517.           l = strlen (argv[op - 1]);
  518.         else
  519.           {
  520.             if (!isint (op - 1, &l))
  521.               int_expt_err ("before -eq");
  522.           }
  523.         if (r_is_l)
  524.           r = strlen (argv[op + 2]);
  525.         else
  526.           {
  527.             if (!isint (op + 1, &r))
  528.               int_expt_err ("after -eq");
  529.           }
  530.         pos += 3;
  531.         return (TRUE == (l == r));
  532.           }
  533.  
  534.         if ('f' == argv[op][2] && '\000' == argv[op][3])
  535.           {
  536.         /* ef - hard link? */
  537.         pos += 3;
  538.         if (l_is_l || r_is_l)
  539.           test_syntax_error ("-ef does not accept -l\n", (char *)NULL);
  540.         if (stat (argv[op - 1], &stat_buf) < 0)
  541.           return (FALSE);
  542.         if (stat (argv[op + 1], &stat_spare) < 0)
  543.           return (FALSE);
  544.         return (TRUE ==
  545.           (stat_buf.st_dev == stat_spare.st_dev &&
  546.            stat_buf.st_ino == stat_spare.st_ino));
  547.           }
  548.         break;
  549.  
  550.       case 'o':
  551.         if ('t' == argv[op][2] && '\000' == argv[op][3])
  552.           {
  553.         /* ot - older than */
  554.         pos += 3;
  555.         if (l_is_l || r_is_l)
  556.           test_syntax_error ("-nt does not accept -l\n", (char *)NULL);
  557.         if (age_of (op - 1, &l) && age_of (op + 1, &r))
  558.           return (TRUE == (l < r));
  559.         return (FALSE);
  560.           }
  561.         break;
  562.     }
  563.       test_syntax_error ("unknown binary operator", argv[op]);
  564.     }
  565.  
  566.     if ('=' == argv[op][0] && '\000' == argv[op][1])
  567.       {
  568.     value = (0 == strcmp (argv[pos], argv[pos + 2]));
  569.     pos += 3;
  570.     return (TRUE == value);
  571.       }
  572.     if ('!' == argv[op][0] && '=' == argv[op][1] && '\000' == argv[op][2])
  573.       {
  574.     value = 0 != strcmp (argv[pos], argv[pos + 2]);
  575.     pos += 3;
  576.     return (TRUE == value);
  577.       }
  578. }
  579.  
  580. static int
  581. unary_operator ()
  582. {
  583.   long r, value;
  584.   struct stat stat_buf;
  585.  
  586.   switch (argv[pos][1])
  587.     {
  588.       default:
  589.         return (FALSE);
  590.  
  591.       /* All of the following unary operators use unary_advance (), which
  592.      checks to make sure that there is an argument, and then advances
  593.      pos right past it.  This means that pos - 1 is the location of the
  594.      argument. */
  595.  
  596.       case 'a':        /* file exists in the file system? */
  597.       case 'e':
  598.     unary_advance ();
  599.     value = -1 != stat (argv[pos - 1], &stat_buf);
  600.     return (TRUE == value);
  601.             
  602.       case 'r':        /* file is readable? */
  603.     unary_advance ();
  604.     value = -1 != eaccess (argv[pos - 1], R_OK);
  605.     return (TRUE == value);
  606.  
  607.       case 'w':        /* File is writeable? */
  608.     unary_advance ();
  609.     value = -1 != eaccess (argv[pos - 1], W_OK);
  610.     return (TRUE == value);
  611.  
  612.       case 'x':        /* File is executable? */
  613.       unary_advance ();
  614.       value = -1 != eaccess (argv[pos - 1], X_OK);
  615.       return (TRUE == value);
  616.  
  617.       case 'O':        /* File is owned by you? */
  618.     unary_advance ();
  619.     if (stat (argv[pos - 1], &stat_buf) < 0)
  620.       return (FALSE);
  621.  
  622.     return (TRUE == (geteuid () == stat_buf.st_uid));
  623.  
  624.       case 'G':        /* File is owned by your group? */
  625.     unary_advance ();
  626.     if (stat (argv[pos - 1], &stat_buf) < 0)
  627.       return (FALSE);
  628.  
  629.     return (TRUE == (getegid () == stat_buf.st_gid));
  630.  
  631.       case 'f':        /* File is a file? */
  632.     unary_advance ();
  633.     if (stat (argv[pos - 1], &stat_buf) < 0)
  634.       return (FALSE);
  635.  
  636.     /* Under USG, -f is true if the given file exists
  637.        and is a regular file.  Other places, this checks
  638.        to see if the given file is not a directory.  The
  639.        Posix.2 spec mandates the System V behavior. */
  640. #if 0
  641.     if (!sys_v)
  642.       return (TRUE != (S_ISDIR (stat_buf.st_mode)));
  643.     else
  644. #endif
  645.       return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
  646.           (0 == (stat_buf.st_mode & S_IFMT))));
  647.  
  648.       case 'd':        /* File is a directory? */
  649.     unary_advance ();
  650.     if (stat (argv[pos - 1], &stat_buf) < 0)
  651.       return (FALSE);
  652.  
  653.     return (TRUE == (S_ISDIR (stat_buf.st_mode)));
  654.  
  655.       case 's':        /* File has something in it? */
  656.     unary_advance ();
  657.     if (stat (argv[pos - 1], &stat_buf) < 0)
  658.       return (FALSE);
  659.  
  660.     return (TRUE == (stat_buf.st_size > (off_t) 0));
  661.  
  662.       case 'S':        /* File is a socket? */
  663. #if !defined (S_ISSOCK)
  664.     return (FALSE);
  665. #else
  666.     unary_advance ();
  667.     if (stat (argv[pos - 1], &stat_buf) < 0)
  668.       return (FALSE);
  669.  
  670.     return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
  671. #endif /* S_ISSOCK */
  672.  
  673.       case 'c': /* File is character special? */
  674.     unary_advance ();
  675.     if (stat (argv[pos - 1], &stat_buf) < 0)
  676.       return (FALSE);
  677.  
  678.     return (TRUE == (S_ISCHR (stat_buf.st_mode)));
  679.  
  680.       case 'b':        /* File is block special? */
  681.     unary_advance ();
  682.     if (stat (argv[pos - 1], &stat_buf) < 0)
  683.       return (FALSE);
  684.  
  685.     return (TRUE == (S_ISBLK (stat_buf.st_mode)));
  686.  
  687.       case 'p':        /* File is a named pipe? */
  688.     unary_advance ();
  689. #ifndef S_ISFIFO
  690.     return (FALSE);
  691. #else
  692.     if (stat (argv[pos - 1], &stat_buf) < 0)
  693.       return (FALSE);
  694.     return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
  695. #endif /* S_ISFIFO */
  696.  
  697.       case 'L':        /* Same as -h  */
  698.       /*FALLTHROUGH*/
  699.  
  700.       case 'h':        /* File is a symbolic link? */
  701.     unary_advance ();
  702. #ifndef S_ISLNK
  703.     return (FALSE);
  704. #else
  705.     if (lstat (argv[pos - 1], &stat_buf) < 0)
  706.       return (FALSE);
  707.  
  708.     return (TRUE == (S_ISLNK (stat_buf.st_mode)));
  709. #endif /* S_IFLNK */
  710.  
  711.       case 'u':        /* File is setuid? */
  712.     unary_advance ();
  713.     if (stat (argv[pos - 1], &stat_buf) < 0)
  714.       return (FALSE);
  715.  
  716.     return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
  717.  
  718.       case 'g':        /* File is setgid? */
  719.     unary_advance ();
  720.     if (stat (argv[pos - 1], &stat_buf) < 0)
  721.       return (FALSE);
  722.  
  723.     return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
  724.  
  725.       case 'k':        /* File has sticky bit set? */
  726.     unary_advance ();
  727.     if (stat (argv[pos - 1], &stat_buf) < 0)
  728.       return (FALSE);
  729. #if ! defined (S_ISVTX)
  730.     /* This is not Posix, and is not defined on some Posix systems. */
  731.     return (FALSE);
  732. #else
  733.     return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
  734. #endif
  735.  
  736.       case 't':        /* File (fd) is a terminal?  (fd) defaults to stdout. */
  737.     advance (0);
  738.     if (pos < argc && isint (pos, &r))
  739.       {
  740.         advance (0);
  741.         return (TRUE == (isatty ((int) r)));
  742.       }
  743.     return (TRUE == (isatty (1)));
  744.  
  745.       case 'n':        /* True if arg has some length. */
  746.     unary_advance ();
  747.     return (TRUE == (0 != strlen (argv[pos - 1])));
  748.  
  749.       case 'z':        /* True if arg has no length. */
  750.     unary_advance ();
  751.     return (TRUE == (0 == strlen (argv[pos - 1])));
  752.     }
  753. }
  754.     
  755. /*
  756.  * and:
  757.  *    and '-a' term
  758.  *    term
  759.  */
  760. static int
  761. and ()
  762. {
  763.   auto int value;
  764.  
  765.   value = term ();
  766.   while ((pos < argc) &&
  767.      ('-' == argv[pos][0]) &&
  768.      ('a' == argv[pos][1]) &&
  769.      ('\000' == argv[pos][2]))
  770.     {
  771.       advance (0);
  772.       value = TRUTH_AND (value, term ());
  773.     }
  774.   return (TRUE == value);
  775. }
  776.  
  777. /*
  778.  * or:
  779.  *    or '-o' and
  780.  *    and
  781.  */
  782. static int
  783. or ()
  784. {
  785.   auto int value;
  786.  
  787.   value = and ();
  788.   while ((pos < argc) &&
  789.      ('-' == argv[pos][0]) &&
  790.      ('o' == argv[pos][1]) &&
  791.      ('\000' == argv[pos][2]))
  792.     {
  793.       advance (0);
  794.       value = TRUTH_OR (value, and ());
  795.     }
  796.   return (TRUE == value);
  797. }
  798.  
  799. /*
  800.  * expr:
  801.  *    or
  802.  */
  803. static int
  804. expr ()
  805. {
  806.   if (pos >= argc)
  807.     beyond ();
  808.  
  809.   return (FALSE ^ (or ()));        /* Same with this. */
  810. }
  811.  
  812. /* Return TRUE if S is one of the test command's binary operators. */
  813. static int
  814. binop (s)
  815.      char *s;
  816. {
  817.   return ((STREQ (s,   "=")) || (STREQ (s,  "!=")) || (STREQ (s, "-nt")) ||
  818.       (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
  819.       (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
  820.       (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
  821. }
  822.  
  823. /* Return non-zerp if OP is one of the test command's unary operators. */
  824. static int
  825. unop (op)
  826.      int op;
  827. {
  828.   return (member (op, "abcdefgkLhprsStuwxOGnz"));
  829. }
  830.  
  831. static int
  832. two_arguments ()
  833. {
  834.   int value;
  835.  
  836.   if (STREQ (argv[pos], "!"))
  837.     value = strlen (argv[pos+1]) == 0;
  838.   else if (argv[pos][0] == '-')
  839.     {
  840.       if (unop (argv[pos][1]))
  841.         value = unary_operator ();
  842.       else
  843.         test_syntax_error ("%s: unary operator expected\n", argv[pos]);
  844.     }
  845.   else
  846.     beyond ();
  847.   return (value);
  848. }
  849.  
  850. static int
  851. three_arguments ()
  852. {
  853.   int value;
  854.  
  855.   if (STREQ (argv[pos], "!"))
  856.     {
  857.       advance (1);
  858.       value = !two_arguments ();
  859.     }
  860.   else if (binop (argv[pos+1]))
  861.     {
  862.       value = binary_operator ();
  863.       pos = argc;
  864.     }
  865.   else if ((STREQ (argv[pos+1], "-a")) || (STREQ (argv[pos+1], "-o")) ||
  866.        (argv[pos][0] == '('))
  867.     value = expr ();
  868.   else
  869.     test_syntax_error ("%s: binary operator expected\n", argv[pos+1]);
  870.   return (value);
  871. }
  872.  
  873. /* This is an implementation of a Posix.2 proposal by David Korn. */
  874. static int
  875. posixtest ()
  876. {
  877.   int value;
  878.  
  879.   switch (argc - 1)    /* one extra passed in */
  880.     {
  881.       case 0:
  882.     value = FALSE;
  883.     pos = argc;
  884.     break;
  885.  
  886.       case 1:
  887.     value = strlen (argv[1]) != 0;
  888.     pos = argc;
  889.     break;
  890.  
  891.       case 2:
  892.         value = two_arguments ();
  893.     pos = argc;
  894.     break;
  895.  
  896.       case 3:
  897.     value = three_arguments ();
  898.     break;
  899.  
  900.       case 4:
  901.         if (STREQ (argv[pos], "!"))
  902.       {
  903.         advance (1);
  904.         value = !three_arguments ();
  905.         break;
  906.       }
  907.     /* FALLTHROUGH */
  908.       case 5:
  909.       default:
  910.     value = expr ();
  911.     }
  912.  
  913.   return (value);
  914. }
  915.  
  916. /*
  917.  * [:
  918.  *    '[' expr ']'
  919.  * test:
  920.  *    test expr
  921.  */
  922. int
  923. #if defined (STANDALONE)
  924. main (margc, margv)
  925. #else
  926. test_command (margc, margv)
  927. #endif /* STANDALONE */
  928.      int margc;
  929.      char **margv;
  930. {
  931.   auto int value;
  932.   int expr ();
  933. #if !defined (STANDALONE)
  934.   int code = setjmp (test_exit_buf);
  935.  
  936.   if (code)
  937.     return (test_error_return);
  938. #endif /* STANDALONE */
  939.  
  940.   argv = margv;
  941.  
  942.   if (margv[0] && strcmp (margv[0], "[") == 0)
  943.     {
  944.       --margc;
  945.  
  946.       if (margc < 2)
  947.     test_exit (SHELL_BOOLEAN (FALSE));
  948.  
  949.       if (margv[margc] && strcmp (margv[margc], "]") != 0)
  950.     test_syntax_error ("missing `]'\n", (char *)NULL);
  951.     }
  952.  
  953.   argc = margc;
  954.   pos = 1;
  955.  
  956.   if (pos >= argc)
  957.     test_exit (SHELL_BOOLEAN (FALSE));
  958.  
  959.   value = posixtest ();
  960.  
  961.   if (pos != argc)
  962.     test_syntax_error ("too many arguments\n", (char *)NULL);
  963.  
  964.   test_exit (SHELL_BOOLEAN (value));
  965. }
  966.